﻿using System;
using UnityEngine;
using UnityEngine.SceneManagement;

// Token: 0x0200022A RID: 554
public class CenterOrigin : MonoBehaviour
{
	// Token: 0x06000BA3 RID: 2979 RVA: 0x00060B05 File Offset: 0x0005ED05
	private void Start()
	{
		CenterOrigin.virtualOrigin = Vector3.zero;
		this.playerCamera = base.GetComponent<PlayerCamera>();
		this.lastUpdate = -100f;
	}

	// Token: 0x06000BA4 RID: 2980 RVA: 0x00060B28 File Offset: 0x0005ED28
	private void LateUpdate()
	{
		this.currentTime = Time.time;
		if (this.currentTime > this.lastUpdate + this.timeDelay)
		{
			this.UpdateOrigin();
		}
	}

	// Token: 0x06000BA5 RID: 2981 RVA: 0x00060B54 File Offset: 0x0005ED54
	private void UpdateOrigin()
	{
		if (this.playerCamera.target == null)
		{
			return;
		}
		Vector3 position = this.playerCamera.target.position;
		if (position.magnitude > this.threshold * this.playerCamera.targetScale)
		{
			this.lastUpdate = this.currentTime;
			this.RpcMoveOrigin(position);
		}
	}

	// Token: 0x06000BA6 RID: 2982 RVA: 0x00060BBC File Offset: 0x0005EDBC
	private void RpcMoveOrigin(Vector3 offset)
	{
		this.rootObjects = SceneManager.GetActiveScene().GetRootGameObjects();
		this.rootTransforms = null;
		if (this.rootTransforms == null)
		{
			this.rootTransforms = new Transform[this.rootObjects.Length];
			for (int i = 0; i < this.rootTransforms.Length; i++)
			{
				this.rootTransforms[i] = this.rootObjects[i].transform;
			}
		}
		for (int j = 0; j < this.rootTransforms.Length; j++)
		{
			this.rootTransforms[j].localPosition -= offset;
		}
		CenterOrigin.virtualOrigin -= offset;
	}

	// Token: 0x06000BA7 RID: 2983 RVA: 0x00060C74 File Offset: 0x0005EE74
	public static Vector3 WorldToVirtual(Vector3 worldPosition)
	{
		worldPosition.x -= CenterOrigin.virtualOrigin.x;
		worldPosition.y -= CenterOrigin.virtualOrigin.y;
		worldPosition.z -= CenterOrigin.virtualOrigin.z;
		return worldPosition;
	}

	// Token: 0x06000BA8 RID: 2984 RVA: 0x00060CCC File Offset: 0x0005EECC
	public static Vector3 WorldToVirtual(float x, float y, float z)
	{
		CenterOrigin.aux.x = x - CenterOrigin.virtualOrigin.x;
		CenterOrigin.aux.y = y - CenterOrigin.virtualOrigin.y;
		CenterOrigin.aux.z = z - CenterOrigin.virtualOrigin.z;
		return CenterOrigin.aux;
	}

	// Token: 0x06000BA9 RID: 2985 RVA: 0x00060D20 File Offset: 0x0005EF20
	public static Vector3 VirtualToWorld(Vector3 virtualPosition)
	{
		virtualPosition.x += CenterOrigin.virtualOrigin.x;
		virtualPosition.y += CenterOrigin.virtualOrigin.y;
		virtualPosition.z += CenterOrigin.virtualOrigin.z;
		return virtualPosition;
	}

	// Token: 0x06000BAA RID: 2986 RVA: 0x00060D78 File Offset: 0x0005EF78
	public static Vector3 VirtualToWorld(float x, float y, float z)
	{
		CenterOrigin.aux.x = x + CenterOrigin.virtualOrigin.x;
		CenterOrigin.aux.y = y + CenterOrigin.virtualOrigin.y;
		CenterOrigin.aux.z = z + CenterOrigin.virtualOrigin.z;
		return CenterOrigin.aux;
	}

	// Token: 0x040010D4 RID: 4308
	private static Vector3 virtualOrigin;

	// Token: 0x040010D5 RID: 4309
	private float threshold = 5000f;

	// Token: 0x040010D6 RID: 4310
	private PlayerCamera playerCamera;

	// Token: 0x040010D7 RID: 4311
	private float timeDelay = 10f;

	// Token: 0x040010D8 RID: 4312
	private float lastUpdate;

	// Token: 0x040010D9 RID: 4313
	private float currentTime;

	// Token: 0x040010DA RID: 4314
	private GameObject[] rootObjects;

	// Token: 0x040010DB RID: 4315
	private Transform[] rootTransforms;

	// Token: 0x040010DC RID: 4316
	private static Vector3 aux;
}
